Hello everybody.
for practise a little bit. i build a basic vendor machine program (code below). i have to questions:
1. Recursion in function chooseproduct and memory usage.
2. Design
1.
As you can see i made a method called "chooseproduct", that allow the user to make a choice of the product it wants to buy. if the choice is a valid product it "returns the product" and then let the user choose another product (like real vendor machines). and there is a exit command and a secret admin option. only when the exit command is entered the program quits.
I am not sure about the recursion I use. the program works as I expected, but if this program would be used on a real vendingmachine it would have less memory recources. and so, I am afraid that the recursion of the chooseproduct function could cause a "out of memory" exception after long time of use (so it wont be quited and there will be a lot of users). Because, i learned that everytime the function calls itself, i would create a copy of the function in the stack. so after 10 times it would have 10 copies of the function chooseproduct in memory, but the function causes a return statement, and when return i called the function will be "dropped" from the memory stack or.... ?
So, waht do you guys/girls think about it, is the recursion no problem because it drops itself caused by return of could we expect a memory problem if this program would run on a real vendingmachine?
2.
I study the basics of C++ it would be very helpfull if you guys/girls would help me with some tips, suggestion, etc. about the program design, pittfalls etc. etc.
Thanks in advanced
Jelte.
main.cpp
Code:
#include "machine.h"
int main()
{
machine objmachine;
return 0;
}
machine.cpp
Code:
#include "machine.h"
machine::machine()
{
menu();
}
machine::~machine()
{
}
void machine::menu() const
{
cout << "producten:" << endl;
cout << "1. cola" << endl;
cout << "2. sinas" << endl;
cout << "3. dubbelfris" << endl;
cout << "4. sprite" << endl;
cout << "5. bosbubbel" << endl;
cout << "99. sluit machine" << endl;
chooseproduct();
}
int machine::chooseproduct(unsigned int chosenproduct) const
{
int product = 0;
if(chosenproduct == 0)
{
cout << "kies product" << endl;
cin >> product;
return chooseproduct(product);
}
else
{
switch(chosenproduct)
{
case cola:
cout << "uitgave product:\tcola" << endl;
return chooseproduct();
case sinas:
cout << "uitgave product:\tsinas" << endl;
return chooseproduct();
case dubbelfris:
cout << "uitgave product:\tdubbelfris" << endl;
return chooseproduct();
case sprite:
cout << "uitgave product:\tsprite" << endl;
return chooseproduct();
case bosbubbel:
cout << "uitgave product:\tbosbubbel" << endl;
return chooseproduct();
case exit:
cout << "machine wordt afgesloten!" << endl;
break;
case beheerder:
cout << "beheer!" << endl;
return chooseproduct();
default:
cout << "ongeldige keuze, kies opnieuw:" << endl;
cin >> product;
return chooseproduct(product);
}
return 0;
}
}
machine.cpp
Code:
#include <iostream>
using namespace std;
class machine
{
public:
machine();
~machine();
private:
enum products {cola = 1, sinas = 2, dubbelfris =3, sprite = 4, bosbubbel = 5 };
enum commands {exit = 99, beheerder = 79315};
void menu() const;
int chooseproduct(unsigned int chosenproduct = 0) const;
};